package com.ruoyi.quartz.task;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.quartz.domain.DeviceInfo;
import com.ruoyi.quartz.domain.DeviceResponse;
import com.ruoyi.quartz.domain.Gps;
import com.ruoyi.quartz.domain.LoginResponse;
import com.ruoyi.quartz.service.ISysJobLogService;
import com.ruoyi.system.domain.DwCar;
import com.ruoyi.system.service.IDwCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.List;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private IDwCarService dwCarService;
    @Autowired
    private ISysJobLogService jobLogService;

    /**
     * 清理定时任务
     */
    public void clearLogs(){
        jobLogService.cleanJobLog(1);
    }
    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
        LoginResponse loginResponse = login();
        if (loginResponse.getCode() == 200) {
            String token = loginResponse.getData().getTokenInfo();
            DeviceResponse deviceResponse = deviceList(token);
            if (deviceResponse.getCode()== 200) {
                List<DeviceInfo> deviceList = deviceResponse.getData();
                for (int i = 0 ; i< deviceList.size(); i ++) {
                    DeviceInfo deviceInfo = deviceList.get(i);
                    DwCar car = dwCarService.selectDwCarByCarNumber(deviceInfo.getDeviceName());
                    if (car != null) {
                        Gps gps = PositionUtil.gps84_To_Gcj02(deviceInfo.getLatest().getLon(),deviceInfo.getLatest().getLat());
                        car.setLatitude(gps.getLatitude());
                        car.setLongitude(gps.getLongitude());
                        car.setGpsTime(DateUtils.dateTime("yyyy-MM-dd HH:mm:ss" ,deviceInfo.getLatest().getGpsTime()));
                        dwCarService.updateDwCar(car);
                    }
                }
            }
        }
    }


    /**
     * 获取天气信息
     * @return
     */
    public LoginResponse login(){
        String url = "http://114.115.130.99:8082/sop-api/user/login";
        JSONObject req = new JSONObject();
        req.put("loginName","qywl");
        req.put("password","e4ce921ccd321f0fd8f0fc3079727e03");
        return restTemplate.postForObject(url,req, LoginResponse.class);
    }

    public DeviceResponse deviceList(String token){
        String url = "http://114.115.130.99:8082/sop-api/device/local/list/groupIds?groupIds=1715";
        HttpHeaders headers = new HttpHeaders();
        headers.add("apiToken", token);
        HttpEntity httpEntity = new HttpEntity<>(headers);
        return restTemplate.exchange(url, HttpMethod.GET, httpEntity ,DeviceResponse.class).getBody();
    }

}
